﻿Imports System.Data.OleDb


Public Class maintenanceGalleryAdd
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If System.Web.HttpContext.Current.User.Identity.IsAuthenticated Then 'This will make sure that if a viewer is not logged in but they enter the link galleryAddImage.aspx, the user validation will fail which will then redirect the user to the unlogged and non adding image page  - gallery.aspx. 


        Else
            Response.Redirect("gallery.aspx")
        End If
    End Sub

    Protected Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click




        If fupimage.HasFile Then

            Dim newFileName As String
            newFileName = Guid.NewGuid().ToString() & getSuffix(fupimage.FileName)
            If fupimage.HasFile Then
                fupimage.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
            End If

            Dim sql As String = "INSERT INTO Maintenance_Images (ImageLink,ImageDescription,DateUploaded,TimeUploaded,UserIDFK) VALUES (@f1,@f2,@f3,@f4,@f5)"

            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

            conn.Open()

            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f1", "Pictures/" & newFileName)
            cmd.Parameters.AddWithValue("@f2", txtimagedescription.Text)
            cmd.Parameters.AddWithValue("@f3", Now.Date.ToString("dd/MM/yyyy"))
            cmd.Parameters.AddWithValue("@f4", DateTime.Now.ToString("t"))
            cmd.Parameters.AddWithValue("@f5", Membership.GetUser(User.Identity.Name).ProviderUserKey)

            cmd.ExecuteNonQuery()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()


            Response.Redirect("maintenanceGalleryAdd.aspx")

        Else
            MsgBox("Sorry, you have not attached an image or / and a simple description")
        End If




    End Sub
    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

End Class